from models import MetricStat
    
def get_all_times(metric_set):
    """
    Given a metric_set object, returns all unique datetimes, sorted in a list.
    """
    all_times = dict([(stat.time_collected, stat.time_collected) for stat in MetricStat.objects.filter(metricdefn__metricset=metric_set)]).values()
    all_times.sort()
    return all_times

def get_all_defn_stats(defns, all_times):
    """
    Given all the defns in a set, and the list of unique times, return corresponding dict:
    Indexed by defn, values are arrays of values in same order as all_times
    """
    temp_stats_table = {}
    for defn in defns:
        temp_stats_table[defn]= dict([(stat.time_collected, stat.value) for stat in MetricStat.objects.filter(metricdefn=defn)])
    
    # Compile into dictionary indexed by defn, values are arrays of values in same order as all_times
    stats_table = dict([(defn, [temp_stats_table[defn].get(t, None) for t in all_times]) for defn in defns])
    return stats_table 
    